package org.surelink.radius.utils;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.surelink.radius.RadiusConfig;
import org.tinyradius.packet.RadiusPacket;

@Component
public class AcctLogger {

    private static final Logger acctlog = Logger.getLogger("acct");

    private static final Logger radiuslog = Logger.getLogger("radius");

    @Autowired
    private RadiusConfig config;

    public void logRequest(RadiusPacket request){

        if(config.getInt("param.logLevel")>=2){
            String msg = String.format("收到记账请求:%s;%s;detail:%s\n",
                    request.getAttribute("NAS-IP-Address"),
                    request.getAttribute("User-Name"),
                    request.toString());

            radiuslog.info(msg);
        }
        else
        {
            String msg = String.format("收到记账请求:%s;%s",
                    request.getAttribute("NAS-IP-Address"),
                    request.getAttribute("User-Name"));

            radiuslog.info(msg);
        }
    }

    public void logResponse(RadiusPacket request,RadiusPacket response){

        if(config.getInt("param.logLevel")>=2){
            String msg = String.format("发送记账响应:%s;detail:%s",
                    request.getAttribute("User-Name"),
                    response.toString());
            radiuslog.info(msg);
        }
        else
        {
            String msg = String.format("发送记账响应:%s",
                    request.getAttribute("User-Name"));
            radiuslog.info(msg);
        }
    }

}
